package com.hcms.admin.controller.cms;

import com.hcms.admin.annotation.CheckPermission;
import com.hcms.admin.dto.CmsCateOptionResponse;
import com.hcms.admin.dto.CmsCateTreeResponse;
import com.hcms.admin.service.ICmsCateService;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.CmsCate;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 栏目管理 前端控制器
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-03-31
 */
@RestController
@RequestMapping("/cmsCate")
@Api("")
public class CmsCateController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ICmsCateService cmsCateService;

    @GetMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<List<CmsCateTreeResponse>>> index() {
        List<CmsCateTreeResponse> cateTrees = cmsCateService.getTreeList();
        return CommonResponse.ok(cateTrees);
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<CmsCate>> view(@PathVariable Long id) {
        CmsCate cate = cmsCateService.getView(id);
        if (cate == null) {
            return CommonResponse.notFound("记录不存在");
        } else {
            return CommonResponse.ok(cate);
        }
    }

    @PostMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<CmsCate>> create(@RequestBody CmsCate entity) {
        logger.info("Run create(), name={}", entity.getCateName());
        int count = cmsCateService.createItem(entity);
        if (count > 0) {
            logger.info("Succeeded in adding record");
            return CommonResponse.created(entity);
        } else {
            logger.error("Failed to add record");
            return CommonResponse.badRequest("添加失败");
        }
    }

    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> update(@PathVariable Long id, @RequestBody CmsCate entity) {
        int count = cmsCateService.updateItem(id, entity);
        if (count > 0) {
            return CommonResponse.accepted("更新成功");
        } else {
            return CommonResponse.badRequest("更新失败");
        }
    }

    @DeleteMapping("/{ids}")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> delete(@PathVariable String ids) {
        int count = cmsCateService.deleteItem(ids);
        if (count > 0) {
            return CommonResponse.noContent("删除成功");
        } else {
            return CommonResponse.badRequest("删除失败");
        }
    }

    @GetMapping("/options")
    @ResponseBody
    @CheckPermission(check = false)
    public ResponseEntity<CommonResponse<List<CmsCateOptionResponse>>> options() {
        List<CmsCateOptionResponse> options = cmsCateService.getOptions();
        return CommonResponse.ok(options);
    }

}
